﻿Imports System.Data.OleDb

Public Class WebForm2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("gozoFarmhouses").ConnectionString)
            Dim SqlString As String = "SELECT * FROM AGENTS WHERE agentUsername=@f1"
            Dim cmd2 As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd2.CommandType = CommandType.Text
            cmd2.Parameters.AddWithValue("@f1", User.Identity.Name)

            oleDbConn.Open()
            Dim agentData As OleDbDataReader = cmd2.ExecuteReader()

            If agentData.HasRows Then

                agentData.Read()

                If Not IsDBNull(agentData("agentName")) Then agent_name.Text = agentData("agentName")
                If Not IsDBNull(agentData("agentSurname")) Then agent_surname.Text = agentData("agentSurname")
                If Not IsDBNull(agentData("agentCompany")) Then agent_company.Text = agentData("agentCompany")
                If Not IsDBNull(agentData("agentPhone")) Then agent_phone.Text = agentData("agentPhone")
                If Not IsDBNull(agentData("agentDescription")) Then agent_description.Text = agentData("agentDescription")
                If Not IsDBNull(agentData("agentFacebookLink")) Then agent_facebook.Text = agentData("agentFacebookLink")
                If Not IsDBNull(agentData("agentTwitterLink")) Then agent_twitter.Text = agentData("agentTwitterLink")
                If Not IsDBNull(agentData("agentSkypeLink")) Then agent_skype.Text = agentData("agentSkypeLink")

            End If
        End If
    End Sub

    Protected Sub submitProfile_Click(sender As Object, e As EventArgs) Handles submitProfile.Click

        Dim Image1GUID As New System.Guid
        Image1GUID = Guid.NewGuid()
        Dim NewImageFileName1 = Image1GUID.ToString() + ".jpg"

        If agent_image.HasFile Then
            Dim picLocation = Request.MapPath("imageUploads") & "/" & NewImageFileName1
            agent_image.SaveAs(picLocation)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("gozoFarmhouses").ConnectionString)

        Dim SqlString As String = "Update Agents SET agentName=@f1, agentSurname=@f2, agentCompany=@f3, agentPhone=@f4, agentDescription=@f5, agentFacebookLink=@f6, agentTwitterLink=@f7, agentSkypeLink=@f8, agentPhoto=@f9 WHERE agentUsername=@f10"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text

        cmd.Parameters.AddWithValue("@f1", agent_name.Text)
        cmd.Parameters.AddWithValue("@f2", agent_surname.Text)
        cmd.Parameters.AddWithValue("@f3", agent_company.Text)
        cmd.Parameters.AddWithValue("@f4", agent_phone.Text)
        cmd.Parameters.AddWithValue("@f5", agent_description.Text)
        cmd.Parameters.AddWithValue("@f6", agent_facebook.Text)
        cmd.Parameters.AddWithValue("@f7", agent_twitter.Text)
        cmd.Parameters.AddWithValue("@f8", agent_skype.Text)
        cmd.Parameters.AddWithValue("@f9", "imageUploads/" & NewImageFileName1)
        cmd.Parameters.AddWithValue("@f10", User.Identity.Name)

        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()

        Response.Redirect("~/default.aspx")

    End Sub
End Class